package com.webex.ta.hydra.gui.action;

import com.webex.ta.hydra.Constants;
import com.webex.ta.hydra.core.Command;
import com.webex.ta.hydra.core.KeywordRegistry;
import com.webex.ta.hydra.core.TestPlan;
import com.webex.ta.hydra.gui.GuiPackage;
import com.webex.ta.hydra.gui.project.HydraProjectSuite;
import com.webex.ta.hydra.gui.util.FileDialoger;
import com.webex.ta.hydra.save.SaveService;
import com.webex.ta.hydra.util.HydraUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.*;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-8-30
 * Time: 11:14:07
 */
public class LoadAction implements ActionHandler {
    private static Set<String> commands = new HashSet<String>();

    static {
        commands.add(ActionNames.OPEN);
    }

    public Set<String> getActionNames() {
        return commands;
    }

    public void doAction(ActionEvent e) {
        JFileChooser chooser = FileDialoger.promptToOpenFile(new String[]{Constants.HYDRA_FILE_EXTENSION});
        if (chooser == null) {
            return;
        }
        File selectedFile = chooser.getSelectedFile();
        if (selectedFile != null) {
            loadProjectFile(selectedFile);
        }
    }

    static void loadProjectFile(File f) {
        GuiPackage gp = GuiPackage.getInstance();
        HydraProjectSuite openedProject = alreadyOpened(f);
        if (openedProject != null) {
            // already opened, acivate it
            gp.getIdeFrame().activateProject(openedProject);
            return;
        }

        InputStream istream = null;
        try {
            istream = new FileInputStream(f);
            TestPlan tp = SaveService.loadTestPlan(istream);

            gp.setTestPlanFile(f.getAbsolutePath());
            gp.getIdeFrame().projectFileLoaded(f.getAbsolutePath());

            preloadMacros(tp.getRoot());

            HydraProjectSuite project = new HydraProjectSuite();
            project.setFile(f);
            GuiPackage.getInstance().getIdeFrame().addProject(project);
            project.getModel().setTestPlan(tp);
            project.takeSnapshot();
            project.getTable().adjustColumnCount();
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            HydraUtils.closeQuietly(istream);
        }
    }

    private static void preloadMacros(Command cmd) {
        if ("LoadMacro".equals(cmd.getKeyword())) {
            KeywordRegistry.getInstance().loadMacros(cmd.getParameter(0));
        }

        if (cmd.hasChild()) {
            for (Command child : cmd.getChildren()) {
                preloadMacros(child);
            }
        }
    }


    private static HydraProjectSuite alreadyOpened(File f) {
        for (HydraProjectSuite project : GuiPackage.getInstance().getIdeFrame().getProjects()) {
            if (project.getFile() != null && project.getFile().equals(f)) {
                return project;
            }
        }
        return null;
    }
}
